package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.vo.wx.WXCommentCountShowVO;
import com.abaaba.project2.bean.vo.wx.WXCommentListShowVO;
import com.abaaba.project2.mapper.CommentMapper;
import com.abaaba.project2.mapper.UserMapper;
import com.abaaba.project2.service.wx.WXCommentListShowService;
import com.abaaba.project2.util.DateToStringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXCommentListShowServiceImpl implements WXCommentListShowService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    static int i = -1;
    static int j = -1;
    @Override
    public WXCommentCountShowVO wxCommentCountShow(WXCommentAccept wxCommentAccept) {

         int allCount =  commentMapper.selectByIdCount(wxCommentAccept.getValueId());
         int hasPicCount =  commentMapper.selectByPicCount(wxCommentAccept.getValueId());
        WXCommentCountShowVO.DataBean wxdatabean  = new WXCommentCountShowVO.DataBean(hasPicCount,allCount);

        WXCommentCountShowVO wxCommentCountShowVO = new WXCommentCountShowVO(0,wxdatabean,"成功");
        return wxCommentCountShowVO;
    }

    @Override
    public WXCommentListShowVO wxCommentListShow(WXCommentAccept wxCommentAccept) {
        PageHelper.startPage(wxCommentAccept.getPage(),wxCommentAccept.getSize());

        CommentExample commentExample = new CommentExample();
        CommentExample commentExampleTopic = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        CommentExample.Criteria criteriaTopic = commentExampleTopic.createCriteria();

        commentExampleTopic.setOrderByClause("add_time"+" "+"desc");
        commentExample.setOrderByClause("add_time"+" "+"desc");
        /*根据商品ID查评论*/
        criteria.andValueIdEqualTo(wxCommentAccept.getValueId());


        UserExample userExample = new UserExample();
        UserExample.Criteria criteria1 = userExample.createCriteria();
        /*根据用户id查询商品信息*/

        List<Comment> commentsTopic =null;
        if (wxCommentAccept.getType() == 1){
            /*说明是从评论取的*/
            criteriaTopic.andValueIdEqualTo(wxCommentAccept.getValueId()).andTypeEqualTo(wxCommentAccept.getType());
            commentsTopic = commentMapper.selectByExample(commentExampleTopic);
        }
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        ArrayList<WXCommentListShowVO.DataBeanX.DataBean> dataBeans = new ArrayList<>();
        ArrayList<WXCommentListShowVO.DataBeanX.DataBean> dataBeans2 = new ArrayList<>();

        ArrayList<WXCommentListShowVO.DataBeanX.DataBean> dataBeansTopic = new ArrayList<>();

        for (Comment comment : comments) {
            DateToStringUtil dateToStringUtil = new DateToStringUtil();
            String commentDate = dateToStringUtil.dateToString(comment.getAddTime());
            if (comment.getHasPicture() == true){
                /*如果有图片*/
                User users = userMapper.selectByPrimaryKey(comment.getUserId());
                WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean userInfoBean = new WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean(users.getNickname(),users.getAvatar());
                WXCommentListShowVO.DataBeanX.DataBean dataBean = new WXCommentListShowVO.DataBeanX.DataBean(userInfoBean,commentDate,comment.getContent(),comment.getPicUrls());
                dataBeans2.add(dataBean);
            }
            User users = userMapper.selectByPrimaryKey(comment.getUserId());
            WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean userInfoBean = new WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean(users.getNickname(),users.getAvatar());
            WXCommentListShowVO.DataBeanX.DataBean dataBean = new WXCommentListShowVO.DataBeanX.DataBean(userInfoBean,commentDate,comment.getContent(),comment.getPicUrls());
            dataBeans.add(dataBean);
        }

        if (wxCommentAccept.getType() == 1){
            for (Comment comment : commentsTopic) {
                DateToStringUtil dateToStringUtil = new DateToStringUtil();
                String commentDate = dateToStringUtil.dateToString(comment.getAddTime());
                if (comment.getHasPicture() == true){
                    /*如果有图片*/
                    User users = userMapper.selectByPrimaryKey(comment.getUserId());
                    WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean userInfoBean = new WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean(users.getNickname(),users.getAvatar());
                    WXCommentListShowVO.DataBeanX.DataBean dataBean = new WXCommentListShowVO.DataBeanX.DataBean(userInfoBean,commentDate,comment.getContent(),comment.getPicUrls());
                    dataBeans2.add(dataBean);
                }
                User users = userMapper.selectByPrimaryKey(comment.getUserId());
                WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean userInfoBean = new WXCommentListShowVO.DataBeanX.DataBean.UserInfoBean(users.getNickname(),users.getAvatar());
                WXCommentListShowVO.DataBeanX.DataBean dataBean = new WXCommentListShowVO.DataBeanX.DataBean(userInfoBean,commentDate,comment.getContent(),comment.getPicUrls());
                dataBeansTopic.add(dataBean);
            }
        }
        /*这个是所有的*/
        int allCount =  commentMapper.selectByIdCount(wxCommentAccept.getValueId());

        /*这个是有图片的*/
        int allCount2 =  commentMapper.selectByPicCount(wxCommentAccept.getValueId());

        Integer allCountTopic =0;
        if (wxCommentAccept.getType() == 1) {
            allCountTopic = commentMapper.selectByPicCount(wxCommentAccept.getValueId());
        }

        int currentPage = 1;
        WXCommentListShowVO.DataBeanX dataBeanX =null;

        if (wxCommentAccept.getShowType() ==1){
            /*有图片就装这个*/
            dataBeanX = new WXCommentListShowVO.DataBeanX(allCount2,currentPage,dataBeans2);
        }else if (wxCommentAccept.getShowType() ==0){
            /*没有图片就装这个*/
            dataBeanX = new WXCommentListShowVO.DataBeanX(allCount,currentPage,dataBeans);
        }
        if (wxCommentAccept.getType() == 1) {
            /*是评论就装这个*/
            dataBeanX = new WXCommentListShowVO.DataBeanX(allCountTopic,currentPage,dataBeansTopic);
        }

        //ArrayList<WXCommentListShowVO.DataBeanX.DataBean> dataBeans = new ArrayList<>();
        /*查进来商品ID，根据商品ID查询用户ID*/
        WXCommentListShowVO wxCommentListShowVO = new WXCommentListShowVO(0,dataBeanX,"成功");

        if (wxCommentAccept.getShowType() ==1){
            j=-1;
        if (wxCommentAccept.getPage()+i++>=(allCount/wxCommentAccept.getSize())+2){
            return null;
        }
        }
            /*没有图片就装这个*/
        if (wxCommentAccept.getShowType() ==0){
            i = -1;
        if (wxCommentAccept.getPage()+j++>=(allCount2/wxCommentAccept.getSize())+2){
                return null;
        }
        }
        return wxCommentListShowVO;
    }
}
